package com.sxfq.loansupermarket.backsystem.modular.base;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONObject;

@RestController
public class UploadController {
	/**
	 * 文件上传
	 */
	@RequestMapping(value = "uploadFile_ajax",produces = {"text/html;charset=UTF-8"})
	public Map<String, Object> uploadPic(@RequestParam(required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response,
			String id,String broadHeading,@RequestParam(required=false) String kindFile) {
		response.setContentType("text/html;charset=UTF-8");
		System.out.println(file.getOriginalFilename());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Calendar now=Calendar.getInstance();
		String uploadfile = request.getParameter("uploadfile-1");
		if(uploadfile!=null&&file.getOriginalFilename()==""){
			resultMap.put("id", id);
			resultMap.put("result", 1);
			resultMap.put("wjlj", uploadfile);
		}else{
			if (file.getOriginalFilename() != null && file.getOriginalFilename().split("\\.").length > 1) {
				String type =file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
				if((type.equals("doc")||type.equals("docx"))&&kindFile==null){
					/*if ((id == null || id.equals(""))) {
						id = UUID.randomUUID().toString();
					}
					Random random=new Random();
					// 如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
					String realPath = "C:\\skyoa\\upload\\"+broadHeading+"\\"+now.get(Calendar.MINUTE)+"\\"+now.get(Calendar.SECOND)+"\\"+random.nextInt(100);
					try {
						FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename()));
						resultMap.put("id", id);
						resultMap.put("result", 1);
						resultMap.put("wjlj", realPath + "\\" + file.getOriginalFilename());
					} catch (IOException e) {
						resultMap.put("result", 0);
						resultMap.put("Exception", e.getMessage());
					}*/
					getUpload(id, broadHeading, file, now, resultMap);
				}else if(kindFile!=null&&kindFile.equals("all")){
					getUpload(id, broadHeading, file, now, resultMap);
				}else{
					resultMap.put("result", -1);
				}
				
			} else {
				resultMap.put("result", 0);
			}
		}
		String json = JSONObject.fromObject(resultMap).toString();
		try {
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	private void getUpload(String id,String broadHeading, MultipartFile file,Calendar now,Map<String, Object> resultMap){
		if ((id == null || id.equals(""))) {
			id = UUID.randomUUID().toString();
		}
		Random random=new Random();
		// 如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
		String realPath = "C:\\skyoa\\upload\\"+broadHeading+"\\"+now.get(Calendar.MINUTE)+"\\"+now.get(Calendar.SECOND)+"\\"+random.nextInt(100);
		try {
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename()));
			resultMap.put("id", id);
			resultMap.put("result", 1);
			resultMap.put("wjlj", realPath + "\\" + file.getOriginalFilename());
		} catch (IOException e) {
			resultMap.put("result", 0);
			resultMap.put("Exception", e.getMessage());
		}
	}
	
}
